import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI
from utils.manager import Images


class Test:
    app = Settings
    system_ui = SystemUI

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击设置应用')
        device.set_brightness(1)
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.进入设置应用')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击声音')
        device.click(*self.app.sounds_item)
        logging.info('预期结果2.进入声音')
        device.refresh_layout()
        device.assert_text_exist('声音')
        device.assert_text_exist('声音模式')
        device.assert_type_exist('Radio')
        device.assert_text_exist('音量')
        device.assert_text_exist('来电')
        device.assert_text_exist('媒体')
        device.assert_text_exist('通话')
        device.assert_type_exist('Slider')

        logging.info('步骤3.选择响铃/静音')
        ring_element = device.get_element_by_type('Radio', index=0)
        ring_checked = ring_element.get('checked')
        mute_element = device.get_element_by_type('Radio', index=1)
        mute_checked = mute_element.get('checked')
        # 切换到静音模式
        if ring_checked == 'true':
            device.click_element(mute_element)
        logging.info('预期结果3.可正常切换，选择响铃手机来电时响起铃声；选择静音上层出现静音图标，且手机来电时静音')
        device.refresh_layout()
        ring_element = device.get_element_by_type('Radio', index=0)
        ring_checked1 = ring_element.get('checked')
        mute_element = device.get_element_by_type('Radio', index=1)
        mute_checked1 = mute_element.get('checked')
        assert ring_checked1 == 'false', '开关切换异常'
        assert mute_checked1 == 'true', '开关切换异常'
        statusbar_pic = device.save_snapshot_to_local('statusbar.jpeg')
        statusbar_pic = Images.crop_picture(statusbar_pic, *self.system_ui.statusbar_area)
        is_in = Images.is_image_in_image(self.system_ui.mute_icon, statusbar_pic)
        assert is_in, '状态栏没有静音图标'

        logging.info('步骤4.返回后再次进入声音设置项')
        device.go_back()
        device.click(*self.app.sounds_item)
        device.refresh_layout()
        ring_element = device.get_element_by_type('Radio', index=0)
        ring_checked2 = ring_element.get('checked')
        mute_element = device.get_element_by_type('Radio', index=1)
        mute_checked2 = mute_element.get('checked')
        assert ring_checked2 == 'false', '退出后再次进入声音设置项，声音开关又还原了'
        assert mute_checked2 == 'true', '退出后再次进入声音设置项，声音开关又还原了'
